<?php echo $header; ?>
<div id="content">
  <div class="breadcrumb">
    <?php foreach ($breadcrumbs as $breadcrumb) { ?>
    <?php echo $breadcrumb['separator']; ?>
    	<?php if(isset($breadcrumb['href']) && $breadcrumb['href'] != '' && $breadcrumb['href'] != '#') {?>
	    	<a href="<?php echo $breadcrumb['href']; ?>">
	    		<?php echo $breadcrumb['text']; ?>
	    	</a>
    	<?php }else{?>
    		<?php echo $breadcrumb['text']; ?>
    	<?php }?>
    <?php } ?>
  </div>
  <?php if ($error_warning) { ?>
  <div class="warning"><?php echo $error_warning; ?></div>
  <?php } ?>
  <?php if ($success) { ?>
  <div class="success"><?php echo $success; ?></div>
  <?php } ?>
  <div class="box">
    <div class="heading">
      <h1><img src="view/image/category.png" alt="" /> <?php echo $heading_title; ?></h1>
      <div class="buttons"><a onclick="location = '<?php echo $insert; ?>'" class="button"><?php echo $button_insert; ?></a><a onclick="$('#form').submit();" class="button"><?php echo $button_delete; ?></a></div>
    </div>
    <div class="content">
      <form action="<?php echo $delete; ?>" method="post" enctype="multipart/form-data" id="form">
      	<div style="float:right">
      		<select name="filter_type" onchange="filter();">
             	<option value="*"><?php echo $text_none; ?></option>
                <?php foreach ($news_types as $type) { ?>
                <?php if ($type['type_id'] == $filter_type) { ?>
                <option value="<?php echo $type['type_id']; ?>" selected="selected"><?php echo $type['name']; ?></option>
                <?php } else { ?>
                <option value="<?php echo $type['type_id']; ?>" ><?php echo $type['name']; ?></option>
                <?php } ?>
                <?php } ?>
            </select>      		
      	</div>
      	<br/>&nbsp;
        <table class="list">
          <thead>
            <tr>
              <td width="1" style="text-align: center;"><input type="checkbox" onclick="$('input[name*=\'selected\']').attr('checked', this.checked);" /></td>
              <td class="left">
            	<?php if ($sort == 'cd.`name`') { ?>
                <a href="<?php echo $href_sort_name; ?>" class="<?php echo strtolower($order); ?>"><?php echo $column_name; ?></a>
                <?php } else { ?>
                <a href="<?php echo $href_sort_name; ?>"><?php echo $column_name; ?></a> 
                <?php } ?>
              </td>
              <td class="right"  style="width: 120px">
              	<?php if ($sort == 'c.sort_order') { ?>
                <a href="<?php echo $href_sort_order; ?>" class="<?php echo strtolower($order); ?>"><?php echo $column_sort_order; ?></a>
                <?php } else { ?>
                <a href="<?php echo $href_sort_order; ?>"><?php echo $column_sort_order; ?></a> 
                <?php } ?>
              </td>
              <td class="center" style="width: 100px">
              	<?php if ($sort == 'c.status') { ?>
                <a href="<?php echo $href_sort_status; ?>" class="<?php echo strtolower($order); ?>"><?php echo $entry_status; ?></a>
                <?php } else { ?>
                <a href="<?php echo $href_sort_status; ?>"><?php echo $entry_status; ?></a> 
                <?php } ?>
              </td>
              <td class="center" style="width: 100px"><?php echo $column_action; ?></td>
            </tr>
          </thead>
          <tbody>
          	<tr class="filter">
              <td></td>
              <td><input type="text" name="filter_name" value="<?php echo $filter_name; ?>" /></td>
              <td></td>
              <td>
              	<select name="filter_status">
                  <option value="*"><?php echo $text_none; ?></option>
                  <?php foreach ($status_list as $status) { ?>
                  <?php if ($status['value'] == $filter_status) { ?>
                  <option value="<?php echo $status['value']; ?>" selected="selected"><?php echo $status['name']; ?></option>
                  <?php } else { ?>
                  <option value="<?php echo $status['value']; ?>" ><?php echo $status['name']; ?></option>
                  <?php } ?>
                  <?php } ?>
                </select>
              </td>
              <td class="center"><a onclick="filter();" class="button" ><?php echo $button_filter; ?></a></td>
            </tr>
            <?php if ($categories) { ?>
            <?php foreach ($categories as $category) { ?>
            <tr>
              <td style="text-align: center;"><?php if ($category['selected']) { ?>
                <input type="checkbox" name="selected[]" value="<?php echo $category['category_id']; ?>" checked="checked" />
                <?php } else { ?>
                <input type="checkbox" name="selected[]" value="<?php echo $category['category_id']; ?>" />
                <?php } ?></td>
              <td><?php echo $category['category_name']; ?></td>
              <td class="right"><?php echo $category['sort_order']; ?></td>
              <td class="center">
              	<?php $status = 'text_status_' . $category['status']; echo $$status; ?>
              </td>              
              <td class="center"><?php foreach ($category['action'] as $action) { ?>
                [ <a href="<?php echo $action['href']; ?>"><?php echo $action['text']; ?></a> ]
                <?php } ?>
              </td>
            </tr>
            <?php } ?>
            <?php } else { ?>
            <tr>
              <td class="center" colspan="6"><?php echo $text_no_results; ?></td>
            </tr>
            <?php } ?>
          </tbody>
        </table>
      </form>
      <div class="pagination"><?php echo $pagination; ?></div>
    </div>
  </div>
</div>

<script type="text/javascript">
function filter() {
	url = 'index.php?route=news/category&token=<?php echo $token; ?>';

	var filter_name = $('input[name=\'filter_name\']').val();	
	if (filter_name) {
		url += '&filter_name=' + encodeURIComponent(filter_name);
	}	
	var filter_type = $('select[name=\'filter_type\']').val();
	if (filter_type != '*') {
		url += '&filter_type=' + encodeURIComponent(filter_type);
	}
	var filter_status = $('select[name=\'filter_status\']').val();	
	if (filter_status != '*') {
		url += '&filter_status=' + encodeURIComponent(filter_status);
	}

	location = url;
}

$(document).ready( function() {
	var filter_name = $('input[name=\'filter_name\']');
	filter_name.focus();		
	filter_name.autocomplete({
		delay: 0,
		source: function(request, response) {
			$.ajax({
				url: 'index.php?route=news/category/autocompleteCategoryName&token=<?php echo $token; ?>&filter_name=' +  encodeURIComponent(request.term),
				dataType: 'json',
				success: function(json) {		
					response($.map(json, function(item) {
						return {
							label: item.category_name,
							value: item.category_id
						}
					}));
				}
			});
		}, 
		select: function(event, ui) {
			$('input[name=\'filter_name\']').val(ui.item.label);
							
			return false;
		}
	});
});
</script>
<?php echo $footer; ?>